package com.study.chunguard.common.utils.easyexcel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * 布尔数字'0'转为汉字'是'，布尔数字'1'转为汉字'否'
 *
 * @author zhaowenhao
 */
public class BooleanNumberCnReverseConverter implements Converter<String> {

    public static final String TRUE_NUMBER = "0";

    public static final String FALSE_NUMBER = "1";

    public static final String TRUE_CN = "是";

    public static final String FALSE_CN = "否";


    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        if (TRUE_CN.equals(cellData.getStringValue())) {
            return TRUE_NUMBER;
        }
        return FALSE_NUMBER;
    }

    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        if (TRUE_NUMBER.equals(value)) {
            return new WriteCellData<>(TRUE_CN);
        }
        return new WriteCellData<>(FALSE_CN);
    }

}
